﻿namespace LiteBlog.Repositories.Helpers
{
    using System;
    using System.Linq;
    using System.Xml;
    using System.Xml.Linq;

    using LiteBlog.Common;
    using LiteBlog.Common.Components;
    using System.Text;
    using LiteBlog.Repositories.Contracts;
    using System.IO;
    using LiteBlog.Repositories.Common;

    public class XmlHelper : IXmlHelper
    {
        public void Save(XElement root, string path)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(path, settings);
            root.Save(writer);
            writer.Close();
        }

        public XElement Load(string path)
        {
            return XElement.Load(path);
        }

        public string GetInnerXml(XElement elem)
        {
            StringBuilder innerXml = new StringBuilder();
            elem.Nodes().ToList().ForEach(node => innerXml.Append(node.ToString()));
            return innerXml.ToString();
        }

        public void Delete(string path)
        {
            if (File.Exists(path))
                File.Delete(path);
        }
    }
}